const cloud = require('wx-server-sdk')
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
    try {
        const { keyword } = event

        const db = cloud.database()
        const _ = db.command

        // 查询匹配的模板数据
        const res = await db.collection('resume_templates')
            .where({
                $or: [
                    { title: db.RegExp({ regexp: keyword, options: 'i' }) },
                    { tags: db.RegExp({ regexp: keyword, options: 'i' }) }
                ]
            })
            .get()

        return {
            code: 0,
            message: 'success',
            data: res.data
        }
    } catch (err) {
        console.error('查询模板失败:', err)
        return {
            code: -1,
            message: err.message
        }
    }
}